#include "listReader.h"

listReader::listReader()
{
    _xdoc = NULL;
}

bool listReader::load(QString fn)
{
    if (!_xdoc)
    {
        _xdoc = new QXlsx::Document(fn);
    }
    else
    {
        delete _xdoc;
        _xdoc = new QXlsx::Document(fn);
    }

    _rowstart = _xdoc->dimension().firstRow();
    _colstart = _xdoc->dimension().firstColumn();
    _rowcur = _rowstart;
    _header.clear();

    for (int column = _colstart;
             column <= _xdoc->dimension().columnCount();
             column++)
    {
        QXlsx::Cell *cell = _xdoc->cellAt(_rowstart, column); //获取单元格
        if (cell)
        {
            _header.append(cell->value().toString());
        }
    }

    return true;
}

bool listReader::next(QMap <QString, QString> &val)
{
    int index = 0;

    if (!_xdoc)
        return false;

    for (int column = _colstart;
         column <= _xdoc->dimension().columnCount();
         column++)
    {
        QXlsx::Cell *cell = _xdoc->cellAt(_rowcur + 1, column);
        if (cell)
        {
            val[_header.at(index)] = cell->value().toString();
            index ++;
        }
    }

    if (index)
        _rowcur ++;

    return true;
}
